/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sdc.strategy.proposal;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import sdc.utility.MersenneTwisterRNG;
/**
 *
 * @author yamazaki
 */
public class ProbMatrixStrategy {

    public List<Integer> CreateFirstUse(double[][] matrix, int firstUseNum){
        MersenneTwisterRNG rnd = MersenneTwisterRNG.INSTANCE;
        List<Integer> usedList = new ArrayList<Integer>();
        for(int i=0;i<firstUseNum;){
            int r1 = rnd.nextInt(matrix.length);
            if(matrix[r1][r1] == 0){
                matrix[r1][r1] = -1;
                i++;
                usedList.add(r1);
            }
        }
        return usedList;
    }

    public List<Integer> createProb(List<Integer> usedList, double[][] matrix){
        MersenneTwisterRNG rnd = MersenneTwisterRNG.INSTANCE;
        List<Integer> freeList = new ArrayList<Integer>();
        for(int i=0;i<matrix[0].length;i++){
            if(!usedList.contains(i)){
                freeList.add(i);
            }
        }
        Set<Integer> usedSet= new HashSet<Integer>();
        int size = freeList.size();
        while (usedSet.size() < size){
            while (true){
                int r1 = freeList.get(rnd.nextInt(freeList.size()));
                int r2 = freeList.get(rnd.nextInt(freeList.size()));
                if (r1!=r2 && matrix[r1][r2]==0.0 && matrix[r2][r1]==0.0){
                    matrix[r1][r2] = rnd.nextDouble();
                    usedSet.add(r1);
                    usedSet.add(r2);
                    freeList.remove((Integer)r2);
                    break;
                }
            }
        }
        return freeList;
    }

    public void createConnect(double[][] matrix, List<Integer> usedList, List<Integer> freeList){
        MersenneTwisterRNG rnd = MersenneTwisterRNG.INSTANCE;
        for(int type : usedList.subList(0, usedList.size())){
            matrix[type][freeList.get(type % freeList.size())] = rnd.nextDouble();
        }
    }

}
